<?php 
$reqPath='';
//require_once("include/authen.php");
require_once($reqPath."class/m_report.php");
require_once($reqPath.'fpdf/fpdf.php');

class PDF extends FPDF
{
	var $datefrom;
	var $dateto;
	
	function setHeaderCriteria($dtfrom , $dtto){
		$this->datefrom=$dtfrom;
		$this->dateto=$dtto;
	}
	function Header()
	{
		//Arial bold 15
		$this->SetFont('tahoma','B',12);
		//Title
		$this->Cell(0,10,iconv( 'UTF-8','cp874' , 'รายงานตัดคอร์สข้ามสาขา'),0,0,'C');
		$this->Ln(5);
		$this->SetFont('tahoma','B',11);
		$this->Cell(0,10,iconv( 'UTF-8','cp874' , 'บริการตั้งแต่ '. $this->datefrom .' ถึง '. $this->dateto),0,0,'C');
		//Line break
		$this->Ln(15);
	}

	//Page footer
	function Footer()
	{
		//Position at 1.5 cm from bottom
		$this->SetY(-15);
		//Arial italic 8
		$this->SetFont('Arial','I',8);
		//Page number
		$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'R');
	}

	//Colored table
	function GenTable($header,$data=null)
	{
		$w=array(45,45,30,25,15,15,25);
		$bln="";
		//header
		$icount=count($data);
		$k=1;
		foreach ($data as $h){
			$this->SetTextColor(0);
			
			if ($bln != $h["BranchService"]){
				if ($bln !=""){
					$this->Cell(array_sum($w),0,'','T');
					$this->Ln();
					$this->SetFillColor(252,209,132);
					$this->Cell(array_sum($w),7,iconv( 'UTF-8','cp874' ,'รวมเงินที่ต้องจ่ายให้แต่ละสาขา'),'',0,'L', true);
					$this->Ln();
					$this->Cell(array_sum($w),0,'','T');
					$this->Ln();
					
					$this->SetFillColor(20,114,218);
					$this->SetTextColor(255);
					$this->Cell($w[0] + $w[1]  ,6,iconv( 'UTF-8','cp874' ,'สาขา' ),'LR',0,'C',true);
					$this->Cell($w[2] + $w[3] ,6,iconv( 'UTF-8','cp874' , 'จำนวนที่ใช้บริการ'),'LR',0,'C',true);
					$this->Cell($w[4] + $w[5] + $w[6]  ,6,iconv( 'UTF-8','cp874' , 'จำนวนเงิน'),'LR',0,'C',true);
					$this->Ln();

					$this->SetTextColor(0);
					$this->SetFillColor(224,235,255);
					$sumfill=false;
					foreach ($summarylist as $sum){
						$this->Cell($w[0] + $w[1]  ,6,$sum['BranchName'],'LR',0,'C',$sumfill);
						$this->Cell($w[2] + $w[3] ,6,$sum['c'],'LR',0,'C',$sumfill);
						$this->Cell($w[4] + $w[5] + $w[6]  ,6, number_format($sum['Total'],2,'.',','),'LR',0,'C',$sumfill);
						$this->Ln();
						$sumfill=!$sumfill;
					}
					$this->Cell(array_sum($w),0,'','T');
					$this->Ln(15);

				}
				$this->SetTextColor(0);	
				$this->SetFont('','B');
				$this->Cell(array_sum($w),6, iconv( 'UTF-8','cp874' , "สาขาที่ใช้บริการ   " . $h["BranchService"] ),'',0,'L',false);
				$this->Ln();
				$bln = $h["BranchService"];
	 

			//Colors, line width and bold font
				$this->SetFillColor(20,114,218);
				$this->SetTextColor(255);
				$this->SetDrawColor(114,175,242);
				$this->SetLineWidth(.1);
				$this->SetFont('','B');
				
				for($i=0;$i<count($header);$i++){
					$this->Cell($w[$i],7,iconv( 'UTF-8','cp874' , $header[$i]),1,0,'C',true);
				}	
				$this->Ln();
				
				$this->SetFillColor(224,235,255);
							
				$this->SetTextColor(0);
				$this->SetFont('');
				//detail
				$fill=false;
				 
			}
			
			$this->Cell($w[0],6,iconv( 'UTF-8','cp874' , $h["FirstName"].' '.$h['LastName']),'LR',0,'L',$fill);
			$this->Cell($w[1],6,iconv( 'UTF-8','cp874' , $h["ProductName"]),'LR',0,'L',$fill);
			$this->Cell($w[2],6,iconv( 'UTF-8','cp874' , $h["BranchName"]),'LR',0,'L',$fill);
			$this->Cell($w[3],6, number_format( $h['Prices'] ,2,'.',','),'LR',0,'R',$fill);
			$this->Cell($w[4],6, number_format( $h['Times'] ,0),'LR',0,'R',$fill);
			$this->Cell($w[5],6, number_format( $h['c'] ,0),'LR',0,'R',$fill);
			$this->Cell($w[6],6, number_format( $h['total'] ,2,'.',','),'LR',0,'R',$fill);
			$this->Ln();
			$fill=!$fill;
			
			$summarylist = $h['summary'];
			if ($k==$icount){
			
			$this->Cell(array_sum($w),0,'','T');
					$this->Ln();
					$this->SetFillColor(252,209,132);
					$this->Cell(array_sum($w),7,iconv( 'UTF-8','cp874' ,'รวมเงินที่ต้องจ่ายให้แต่ละสาขา'),'',0,'L', true);
					$this->Ln();
					$this->Cell(array_sum($w),0,'','T');
					$this->Ln();
					
					$this->SetFillColor(20,114,218);
					$this->SetTextColor(255);
					$this->Cell($w[0] + $w[1]  ,6,iconv( 'UTF-8','cp874' ,'สาขา' ),'LR',0,'C',true);
					$this->Cell($w[2] + $w[3] ,6,iconv( 'UTF-8','cp874' , 'จำนวนที่ใช้บริการ'),'LR',0,'C',true);
					$this->Cell($w[4] + $w[5] + $w[6]  ,6,iconv( 'UTF-8','cp874' , 'จำนวนเงิน'),'LR',0,'C',true);
					$this->Ln();
					
					//$summarylist = $h['summary'];
					$this->SetTextColor(0);
					$this->SetFillColor(224,235,255);
					$sumfill=false;
					foreach ($summarylist as $sum){
						$this->Cell($w[0] + $w[1]  ,6,$sum['BranchName'],'LR',0,'C',$sumfill);
						$this->Cell($w[2] + $w[3] ,6,$sum['c'],'LR',0,'C',$sumfill);
						$this->Cell($w[4] + $w[5] + $w[6]  ,6, number_format($sum['Total'],2,'.',','),'LR',0,'C',$sumfill);
						$this->Ln();
						$sumfill=!$sumfill;
					}
					$this->Cell(array_sum($w),0,'','T');
					$this->Ln(15);
			
			}
	
			$k++;
		}
		
	}
}

if (!(isset($_POST['dFrom']))) {
	 header("location:report_course_diff_branch.php");
}

	$datefrom = $_POST['dFrom'];
	$dateto = $_POST['dTo'];
	
	list($day, $month, $year) = split('/', $datefrom);
	$c_from =$year."/".$month."/".$day;

	list($day, $month, $year) = split('/', $dateto);
	$c_to =$year."/".$month."/".$day;

	$branch = $_POST['branch'];

	$report=new M_report();
  //$c_from="2011/03/01";
  //$c_to="2011/04/30";
 // $branch=0;
 	$header=$report->CourseDiff_Header();
	$data=$report->CourseDiff_Detail($c_from, $c_to, $branch);
if (count($data)==0){
	echo "<script>alert('Data not found');window.close();</script>";
	exit();
}
//echo $data;
	$pdf=new PDF();
	$pdf->setHeaderCriteria($datefrom,$dateto);
	$pdf->SetRightMargin(5);
	$pdf->SetLeftMargin(5);
	//Set pageno
	$pdf->AliasNbPages();
	$pdf->AddFont("tahoma","","tahoma.php");
	$pdf->AddFont("tahoma","B","tahomab.php");

	$pdf->SetFont("tahoma","B",10); 
	$pdf->SetFont("tahoma","",10); 

	//Data loading
	$pdf->AddPage();
	$pdf->GenTable($header,$data );
	$pdf->Output();
	exit();
 
?>
